package com.example.blog.Interceptor;

import com.example.blog.common.Utils.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从head中获取token(令牌)
        String jwtToken = request.getHeader("user-token");
        log.info("获取的令牌:"+jwtToken);
        //检验令牌
        Claims claims = JWTUtils.parseJwt(jwtToken);
        if(claims!=null){
            log.info("令牌通过:",claims);
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
